package com.cqdev.washer.api.controller;

import com.cqdev.washer.api.model.InputProject;
import com.cqdev.washer.api.model.ProjectVO;
import com.cqdev.washer.biz.enums.ProjectExecuteStatusEnum;
import com.cqdev.washer.biz.model.DbConfigBO;
import com.cqdev.washer.biz.model.ProjectBO;
import com.cqdev.washer.biz.service.ExecuteService;
import com.cqdev.washer.biz.service.ProjectService;
import com.cqdev.washer.infra.enums.DBDriverType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yunfei.li
 *         类描述: TODO
 *         日期： 2018/6/22
 */
@Controller
public class ProjectController {

    private final ProjectService projectService;

    private final ExecuteService executeService;

    public ProjectController(ProjectService projectService, ExecuteService executeService) {
        this.projectService = projectService;
        this.executeService = executeService;
    }

    @GetMapping("/")
    public String list(Model model) {
        List<ProjectBO> projectBOList = projectService.listAll();

        List<ProjectVO> projectVOList = projectBOList.stream().map(projectBO -> new ProjectVO(
                projectBO.getId(),
                "/project/execute/" + projectBO.getId(),
                projectBO.getName(),
                projectBO.getDbConfigBO().getDbDriverType().name()
        )).collect(Collectors.toList());
        System.out.println("测试测试");
        model.addAttribute("projects", projectVOList);
        return "project";
    }

    @GetMapping("/project/execute/{id}")
    public String execute(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
        ProjectBO projectBO = projectService.getOne(id);

        if (projectBO == null) {
            redirectAttributes.addFlashAttribute("message", "" +
                    "项目不存在！");
            return "redirect:/";
        }

        if (projectBO.getProjectExecuteStatusEnum() == ProjectExecuteStatusEnum.DOING) {
            redirectAttributes.addFlashAttribute("message", "" +
                    "项目正在运行中！");
            return "redirect:/";
        }
        new Thread(() -> {
            executeService.exec(id);
        }).start();
        redirectAttributes.addFlashAttribute("message", "" +
                "启动成功!");
        return "redirect:/";
    }

    @GetMapping("/project/create")
    public String toCreatePage() {
        return "inputProject";
    }

    @PostMapping("/project/create")
    public String createProject(InputProject inputProject) {

        DbConfigBO dbConfigBO = new DbConfigBO(
                inputProject.getUrl(),
                inputProject.getDbName(),
                inputProject.getUsername(),
                inputProject.getPassword(),
                DBDriverType.valueOf(inputProject.getType())
        );
        ProjectBO projectBO = new ProjectBO(
                inputProject.getName2(),
                dbConfigBO
        );
        projectService.save(projectBO);
        return "redirect:/";
    }

}
